package com.zhny.aiot.jdrk.weather.plugin;

import com.zhny.aiot.jdrk.weather.plugin.model.WeatherStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;

public class WeatherDevice extends ModbusNumDevice<WeatherStoreEntry> {
    public WeatherDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (engine, factory, centerFacilities, entity);
    }

    @Override
    public WeatherStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        double speed = modemResponseMessage.getData ().readUnsignedShort ()*0.1;
        int power = modemResponseMessage.getData ().readUnsignedShort ();
        int direction = modemResponseMessage.getData ().readUnsignedShort ();
        int x = modemResponseMessage.getData ().readUnsignedShort ();
        double soilH1 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilT1 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        int soilEC1 = modemResponseMessage.getData ().readUnsignedShort ();
        double soilPH1 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilH2 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilT2 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        int soilEC2 = modemResponseMessage.getData ().readUnsignedShort ();
        double soilPH2 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilH3 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilT3 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        int soilEC3 = modemResponseMessage.getData ().readUnsignedShort ();
        double soilPH3 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double dewPointTemperature = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilH4 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double soilT4 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double sunlightHours = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double lightRainfall = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double airH = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double airT = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double noise = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        int co2 = modemResponseMessage.getData ().readUnsignedShort ();
        double airP = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        int lightH = modemResponseMessage.getData ().readUnsignedShort ();
        int lightL = modemResponseMessage.getData ().readUnsignedShort ();
        int rainAndSnow = modemResponseMessage.getData ().readUnsignedShort ();
        int uvIndex = modemResponseMessage.getData ().readUnsignedShort ();
        int totalRadiation = modemResponseMessage.getData ().readUnsignedShort ();
        int photosyntheticallyActiveRadiation = modemResponseMessage.getData ().readUnsignedShort ();
        double totalRainH = modemResponseMessage.getData ().readUnsignedShort () * 0.2;
        double totalRainL = modemResponseMessage.getData ().readUnsignedShort () * 0.2;
        double currentRain = modemResponseMessage.getData ().readUnsignedShort ()* 0.2;
        double instantaneousRainfall = modemResponseMessage.getData ().readUnsignedShort ()* 0.2;
        double dailyRainfall = modemResponseMessage.getData ().readUnsignedShort ()* 0.2;
        int o3 = modemResponseMessage.getData ().readUnsignedShort ();
        int co = modemResponseMessage.getData ().readUnsignedShort ();
        int so2 = modemResponseMessage.getData ().readUnsignedShort ();
        int no2 = modemResponseMessage.getData ().readUnsignedShort ();
        double o2 = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        double h2s = modemResponseMessage.getData ().readUnsignedShort () * 0.1;
        int evaporationCapacity = modemResponseMessage.getData ().readUnsignedShort ();
        int pm2_5 = modemResponseMessage.getData ().readUnsignedShort ();
        int pm10 = modemResponseMessage.getData ().readUnsignedShort ();
        WeatherStoreEntry entry = new WeatherStoreEntry();
        entry.setWindSpeed (speed);entry.setWindPower (power);
        entry.setWindDirection (direction);entry.setSoilHumidity1 (soilH1);
        entry.setSoilTemperature1 (soilT1);entry.setSoilEC1 (soilEC1);
        entry.setSoilPH1 (soilPH1);entry.setSoilHumidity2 (soilH2);
        entry.setSoilTemperature2 (soilT2);entry.setSoilEC2 (soilEC2);
        entry.setSoilPH2 (soilPH2);entry.setSoilHumidity3 (soilH3);
        entry.setSoilTemperature1 (soilT3);entry.setSoilEC1 (soilEC3);
        entry.setSoilPH1 (soilPH3);entry.setDewPointTemperature (dewPointTemperature);
        entry.setSoilHumidity4 (soilH4);entry.setSoilTemperature4 (soilT4);
        entry.setSunlightHours (sunlightHours);entry.setOpticalRainfall (lightRainfall);
        entry.setAirHumidity (airH);entry.setAirTemperature (airT);
        entry.setNoise (noise);entry.setCo2Concentration (co2);
        entry.setAtmosphericPressure (airP);entry.setIllumination ((lightH << 16 | lightL));
        entry.setRainAndSnow (rainAndSnow == 1?"有雨":"正常");entry.setUvIndex (uvIndex);
        entry.setTotalRadiation (totalRadiation);entry.setPhotosyntheticallyActiveRadiation (photosyntheticallyActiveRadiation);
        entry.setAccumulatedRainfall ((totalRainH + totalRainL));entry.setCurrentRainfall (currentRain);
        entry.setInstantaneousRainfall (instantaneousRainfall);entry.setDailyRainfall (dailyRainfall);
        entry.setO3Concentration (o3);entry.setCoConcentration(co);
        entry.setSo2Concentration (so2);entry.setNo2Concentration (no2);
        entry.setO2Concentration (o2);entry.setH2sConcentration (h2s);
        entry.setEvaporationCapacity (evaporationCapacity);entry.setPm2_5 (pm2_5);
        entry.setPm10 (pm10);

        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 90;
    }

    @Override
    protected IRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
